package com.example.utils;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import io.swagger.models.auth.In;

import java.util.HashMap;
import java.util.Map;

public class OriginConverter implements Converter<String> {
    private static final Map<Integer, String> STATUS_MAP = new HashMap<>();

    static {
        STATUS_MAP.put(35, "烟台");
        // 可以继续添加其他映射关系
        STATUS_MAP.put(51, "杭州");
    }

    @Override
    public Class<String> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                    GlobalConfiguration globalConfiguration) {
        if (cellData.getType() == CellDataTypeEnum.NUMBER) {
            Integer key = cellData.getNumberValue().intValue();
            return STATUS_MAP.getOrDefault(key, cellData.getStringValue());
        }
        return cellData.getStringValue();
    }

    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        try {
            Integer key = Integer.parseInt(value);
            String displayValue = STATUS_MAP.getOrDefault(key, value);
            return new WriteCellData<>(displayValue);
        } catch (NumberFormatException e) {
            return new WriteCellData<>(value);
        }
    }
}
